import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import com.sun.corba.se.impl.ior.ByteBuffer;




public class DataStorer implements Runnable {
	private Queue<ArrayList<Data>> queue;
	private DiskWriter writer;
	private ByteBuffer myBuffer = new ByteBuffer();
	private StringBuffer sb = new StringBuffer();

	public DataStorer() {
		queue = new LinkedList<ArrayList<Data>>();
		writer = new DiskWriter();
	//	new Thread(this).start();
	}

	public void add(Data data) {
		String myBytes = dataParser(data);
		sb.append(myBytes);
		
		writer.addToBuffer(sb);
		sb = new StringBuffer();
	}

	private String dataParser(Data data) {
		byte[] buffer;
		String test;
		StringBuffer end = new StringBuffer();
		String station =  data.getStn() + "";
		String date = data.getDate();
		String time = data.getTime();
		String temp = data.getTemp() + "";
		String dewp = data.getDewp() + "";
		String stp = data.getStp() + "";
		String slp = data.getSlp() + "";
		String visib = data.getVisib() + "";
		String wdsp = data.getWdsp() + "";
		String prcp = data.getPrcp() + "";
		String sndp = data.getSndp() + "";
		String frshtt = data.getFrshtt();
		String cldc = data.getCldc() + "";
		String wnddir = data.getWnddir() + "";

//		buffer = (station + ";" +
//				date + ";" +
//				time + ";" +
//				temp + ";" +
//				dewp + ";" +
//				stp + ";" +
//				slp + ";" +
//				visib + ";" +
//				wdsp + ";" +
//				prcp + ";" +
//				sndp + ";" +
//				frshtt + ";" +
//				cldc + ";" +
//				wnddir + ";\n").getBytes();
		
		test = (toLength(station,6) +
				date.replaceAll("-", "") +
				time.replaceAll(":", "") + 
				toLength(temp,6) +
				toLength(dewp,6) +
				toLength(stp, 6) +
				toLength(slp, 6) + 
				toLength(visib, 5) + 
				toLength(wdsp, 5) +
				toLength(prcp, 6) +
				toLength(sndp.replaceAll("-", ""),6) +
				toLength(frshtt, 6) +
				toLength(cldc,4) +
				toLength(wnddir, 3));
//		System.out.println(toLength(station,6) +
//				date.replaceAll("-", "") +
//				time.replaceAll(":", "") + 
//				toLength(temp,6) +
//				toLength(dewp,6) +
//				toLength(stp, 6) +
//				toLength(slp, 6) + 
//				toLength(visib, 5) + 
//				toLength(wdsp, 5) +
//				toLength(prcp, 6) +
//				toLength(sndp.replaceAll("-", ""),6) +
//				toLength(frshtt, 6) +
//				toLength(cldc,4) +
//				toLength(wnddir, 3));
//		
//		test = (station + ";" +
//				date + ";" +
//				time + ";" +
//				temp + ";" +
//				dewp + ";" +
//				stp + ";" +
//				slp + ";" +
//				visib + ";" +
//				wdsp + ";" +
//				prcp + ";" +
//				sndp + ";" +
//				frshtt + ";" +
//				cldc + ";" +
//				wnddir + ";\n");
		//writer.addToBuffer(buffer);
		return test;

	}

	private Integer negativeBin(Double num) {
		return num < 0 ? 1 : 0;
	}


	private String toLength(String str, int length) {
		while(str.length() < length)
			str = "0" + str;		
		return str;
	}

	@Override
	public void run() {
		ArrayList<Data> data;
		while (true) {
				System.out.println(queue.size());
				try {
					data = queue.poll();
					for (Data d : data) {
						this.dataParser(d);		
					}
				} catch (Exception e) {
					System.out.println(e);
				}
			
		}

	}


}